package com.myplans.server.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.rpc.server.JsonRpcExecutor;
import org.json.rpc.server.JsonRpcServletTransport;

public class PlanshareUserService extends HttpServlet {

	private static final long serialVersionUID = 8994469582398118999L;
	private static Logger logger = Logger.getLogger(PlanshareUserService.class);

//	@Override
//	public void doGet(HttpServletRequest request, HttpServletResponse response) {
//		logger.info("Shouldn't be here!");
////		FileInputStream fis;
//		try {
////			fis = new FileInputStream("DPgXL.jpg");
////			BufferedInputStream bis = new BufferedInputStream(fis);
////			byte[] bytes = new byte[bis.available()];
////			response.setContentType("image/jpeg");
//			OutputStream os = response.getOutputStream();
////			bis.read(bytes);
//			String image = request.getParameter("image");
//			byte[] bytes = Base64.decodeBase64(image.getBytes());
// 			os.write(bytes);
////		} catch (FileNotFoundException e) {
////			// TODO Auto-generated catch block
////			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
//
//	@Override
//	public void doPost(HttpServletRequest request, HttpServletResponse response) {
//
//		logger.info("Processing upload request.");
//        int r;
//        try {
//        	InputStream is = request.getInputStream();
//        	ByteArrayOutputStream baos = new ByteArrayOutputStream();
//			while ((r = is.read()) != -1) {
//			    baos.write(r);
//			}
//			new String(baos.toByteArray());
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//		FileItemFactory fileFactory = new DiskFileItemFactory();
//		ServletFileUpload uploadServlet = new ServletFileUpload(fileFactory);
//		try {
//			List items = uploadServlet.parseRequest(request);
//			Iterator iterator = items.iterator();
//			FileItem item = (FileItem) iterator.next();
//			byte[] itemData = item.get();
//			InputStream in = new ByteArrayInputStream(itemData);
//			BufferedImage bImageFromConvert = ImageIO.read(in);
//			ImageIO.write(bImageFromConvert, "jpg", new File(
//					String.valueOf(System.currentTimeMillis())+".jpg"));
//			response.setContentType("text/html");
//			
//			String s = "data:image/jpg"+";base64," + new String(Base64.encodeBase64(itemData));
//		    response.getOutputStream().write(s.getBytes());
//		    response.getOutputStream().flush();
//		} catch (Exception e) {
//			try {
//				response.getOutputStream().write("Picture upload failed".getBytes());
//				response.getOutputStream().flush();
//			} catch (IOException ex) {
//				ex.printStackTrace();
//			}
//			logger.error("Exception while processing the upload request: " + e.getMessage());
//			e.printStackTrace();
//		}
//		
//	}
	
	private final JsonRpcExecutor executor;

    public PlanshareUserService() {
        executor = bind();
    }

    private JsonRpcExecutor bind() {
        JsonRpcExecutor executor = new JsonRpcExecutor();

        UserService planImpl = new AndroidUserServiceImpl();
        executor.addHandler("user", planImpl, UserService.class);
        // add more services here

        return executor;
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        executor.execute(new JsonRpcServletTransport(req, resp));
    }
}
